/* Copyright (C) 2010-2011, Mamadou Diop.
*  Copyright (C) 2011, Doubango Telecom.
*
* Contact: Mamadou Diop <diopmamadou(at)doubango(dot)org>
*	
* This file is part of imsdroid Project (http://code.google.com/p/imsdroid)
*
* imsdroid is free software: you can redistribute it and/or modify it under the terms of 
* the GNU General Public License as published by the Free Software Foundation, either version 3 
* of the License, or (at your option) any later version.
*	
* imsdroid is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
* See the GNU General Public License for more details.
*	
* You should have received a copy of the GNU General Public License along 
* with this program; if not, write to the Free Software Foundation, Inc., 
* 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/
package org.doubango.ngn.sip;

import org.doubango.ngn.NgnEngine;
import org.doubango.ngn.utils.NgnStringUtils;

/**
 * @author dpGroup
 * Refactoring: Created a new data class NgnMsrpFileData that holds three fields
 * This is created because of a method extracted in NgnMsrpSession class.
 * We can use the same class too but this is a big class so I created a simple
 * new data class just to demonstrate the refactoring technique
 */	
public class NgnMsrpFileData {
	
	private String mFilePath;
	private String mFileName;
	private String mFileType;
		
	
	public String getFileName(){
        return mFileName;
	}

	public String getFilePath() {
		return mFilePath;
	}

	public String getFileType() {
		return mFileType;
	}
	
	public void setFileName(String fileName){
        mFileName = fileName;
	}

	public void setFilePath(String filePath) {
		mFilePath = filePath;
	}

	public void setFileType(String fileType) {
		mFileType = fileType;
	}
	/**
	 * @author dpGroup
	 * Applied Extract Method refactoring & Replace Method with Method Object Refactoring here
	 */	
	public static NgnMsrpFileData getMsrpFileData(String fileSelector)
	{
		NgnMsrpFileData MsrpFileData = new NgnMsrpFileData(); 
		
      // file-selector:name:\"Akav1-MD5.7z\" type:application/octet-stream size:14313 hash:sha-1:48:B4:17:55:DE:3D:6F:45:B1:66:4A:B4:B4:B5:BC:01:AB:0C:A9:E8
         // FIXME: name with spaces will fail
         String[] values = fileSelector.split(" ");
         for(String value : values){
             String[] avp = value.split(":");
             if(avp.length >=2){
                 if(NgnStringUtils.equals(avp[0], "name", true)){
                     MsrpFileData.setFileName(NgnStringUtils.unquote(avp[1], "\""));
                 }
                 else if(NgnStringUtils.equals(avp[0], "type", true)){
                     MsrpFileData.setFileType(avp[1]);
                 }
             }
         }
         MsrpFileData.setFilePath(String.format("%s/%s", 
        		 NgnEngine.getInstance().getStorageService().getContentShareDir(), 
        		 MsrpFileData.getFileName()));
         return MsrpFileData;
	}
}
